package com.example.question.list.list1;

/**
 * @Date 2023-07-03
 * @Author Jonathan
 */
public class Code04_82 {
    public static void main(String[] args) {
        Code04_82 code = new Code04_82();
        ListNode node = List.createList(new int[]{1, 1, 2, 3, 3, 4, 4, 5});
        ListNode ret = code.deleteDuplicates(node);
        node = List.createList(new int[]{1, 1});
        ret = code.deleteDuplicates(node);
        List.printLink(ret);

    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummyHead = new ListNode(-1, head);
        ListNode pre = dummyHead;
        ListNode cur = dummyHead.next;

        while (cur != null && cur.next != null) {
            // 相同元素
            if (cur.val == cur.next.val) {
                do {
                    cur = cur.next;
                } while (cur.next != null && cur.val == cur.next.val);
                // 结束do while 循环的时候 cur指向一组重复元素的最后一个
                // 此时 pre.next = cur.next 即可删除当前重复的元素
                pre.next = cur.next;
            } else {
                // 不是相同元素
                pre = cur;
            }

            cur = pre.next;
        }
        return dummyHead.next;
    }
}
